define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'unit/unit/index',
                    add_url: 'unit/unit/add',
                    edit_url: 'unit/unit/edit',
                    multi_url: 'unit/unit/multi',
                    del_url: 'unit/unit/del',
                    table: 'fa_unit',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                escape: false,
                pk: 'id',
                sortName: 'id', //要使排序生效，在前面添加 weigh desc, 并且数据表weigh字段不能全为0,controller里$list=...->order前再添加->order("weigh desc")
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'province_id', title: '城市'},
                        {field: 'name', title: '合作单位名称'},
                        {field: 'status', title: '状态', operate: false, formatter:Controller.api.formatter.changeStatus},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: '组织结构',
                                    classname: 'btn btn-xs btn-warning btn-click',
                                    icon: 'fa fa-sitemap',
                                    click: function () {
                                        Backend.api.open('/adminht.php/dept/dept?ref=addtabs', '组织结构', {
                                            max: true,
                                            area: ['80%', '80%'],
                                            //offset: ['0px', '0px'],
                                            callback: function (data) {
                                                Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                            }
                                        });
                                    },

                                },
                                {
                                    name: 'detail',
                                    title: '管理员',
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-user-circle',
                                    click: function () {
                                        Backend.api.open('/position/position?ref=addtabs', '管理员', {
                                            max: true,
                                            area: ['80%', '80%'],
                                            //offset: ['0px', '0px'],
                                            callback: function (data) {
                                                Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                            }
                                        });
                                    },

                                }
                            ]
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/add.js';
            document.head.appendChild(script);
            Controller.api.bindevent();
        },
        edit: function () {
            var row = JSON.parse($('#row').val());
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/edit.js';
            document.head.appendChild(script);
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter:{
                changeStatus: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="unit/unit/changeStatus" data-confirm="确认切换状态？" data-id="' + row.id + '"><i class="fa ' + (row.status === 0 ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            }
        }
    };
    return Controller;
});